<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
use think\facade\Config;

Config::load('snake/wechat', 'wechat');

function get_encode_password(string $password)
{
    return md5(base64_encode(md5(md5(base64_encode($password)))));
}

function sign_in_follow_operation(array $adminUserInfo)
{
    set_admin_user_info($adminUserInfo);
    record_admin_login_log(request()->ip());
    set_menu_cache(get_admin_role_menu(get_admin_user_id()));
    return json(200);
}

function get_admin_role_menu(int $admin_id)
{
    $adminRole = \app\controller\AdminUsersRole::getUsersRole(new \app\model\AdminUsersRole(), $admin_id);
    $adminRoleMenu = \app\controller\AdminRoleMenu::getSignInUserMenuPermisson(new \app\model\AdminRoleMenu(), $adminRole[0]['role_id']);
    foreach ($adminRoleMenu as $k => $v)
    {
        $adminMenuData[] = $adminRoleMenu[$k]['menu_list'];
    }
    foreach ($adminMenuData as $menuValue)
    {
        foreach ($menuValue as $childValue)
        {
            $adminInitMenuData[] = $childValue;
        }
    }
    return array_unique($adminInitMenuData, SORT_REGULAR);
}

function update_user_info_follow_operation()
{
    record_log('修改了个人信息');
    \think\facade\Cache::clear();
    return json(200);
}

function set_menu_cache(array $menuData)
{
    cache('admin_menu', $menuData);
    return true;
}

function get_menu_cache()
{
    return cache('admin_menu');
}

function set_admin_user_info(array $adminUserInfo)
{
    cache('admin_user_info', $adminUserInfo);
}

function get_admin_user_info() : array
{
    return cache('admin_user_info');
}

function get_admin_user_id() : int
{
    $adminInfo = get_admin_user_info();
    return $adminInfo['id'];
}

function record_log(string $log)
{
    $systemOperatingLog = new \app\model\SystemOperatingLog();
    $systemOperatingLog->operating = $log;
    return $systemOperatingLog->record();
}

function record_admin_login_log(string $loginIp)
{
    $adminLoginLog = new \app\model\AdminLoginLog();
    $adminLoginLog->loginIp = $loginIp;
    return $adminLoginLog->record();
}

function get_wechat_config()
{
    return array(
        'app_id' => config('wechat.app_id'),
        'secret' => config('wechat.secret'),
        'token'  => config('wechat.token'),
        'response_type' => config('wechat.response_type'),
    );
}